layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 仓库表管理
     */
    var BasicWarehouse = {
        tableId: "basicWarehouseTable"
    };

    /**
     * 初始化表格的列
     */
    BasicWarehouse.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            // {align: 'center',width: 120, toolbar: '#tableBar', title: '操作'},
            {field: 'warehouseOperateName', title: '项目部'},
            {field: 'warehouseName', title: '仓库名称'},
            {field: 'warehouseTypes', title: '仓储类型'},
            {field: 'warehouseType', title: '仓库类型'},
            {field: 'warehouseNumber', title: '仓库编号', sort: true},
            {field: 'warehouseContacts', title: '联系人'},
            {field: 'tel', title: '联系电话'},
            {field: 'warehouseAddress', title: '详细地址'},
            {field: 'longitude', title: '经度'},
            {field: 'latitude', title: '维度'},
            {field: 'warehouseCanmeasure', title: '可用面积', sort: true},
            {field: 'warehouseMeasure', title: '面积', sort: true},
            {field: 'warehouseNotes', title: '备注'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'warehouseXlbId', sort: true, title: '新零帮仓库id'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicWarehouse.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['warehouseOperateId'] = $("#warehouseOperateId").val();
        table.reload(BasicWarehouse.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    BasicWarehouse.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/basicWarehouse/add_hx';
    };

    /**
     * 导出excel按钮
     */
    BasicWarehouse.exportExcel = function () {
        var checkRows = table.checkStatus(BasicWarehouse.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    BasicWarehouse.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/basicWarehouse/edit_hx?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BasicWarehouse.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/basicWarehouse/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BasicWarehouse.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicWarehouse.tableId,
        url: Feng.ctxPath + '/basicWarehouse/list',
        page: true,
        limit: 20,
        height: "full-60",
        cellMinWidth: 100,
        height: 400,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        cols: BasicWarehouse.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicWarehouse.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        BasicWarehouse.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BasicWarehouse.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BasicWarehouse.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BasicWarehouse.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BasicWarehouse.onDeleteItem(data);
        }
    });


    var BasicPutgoods = {
        tableId: "basicPutgoodsTable",    //表格id
        condition: {
            dictId: "",
        }
    };
    BasicPutgoods.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {align: 'center',width: 120, toolbar: '#tableBarPutgoods', title: '操作'},
            {field: 'id', title: '单位ID'},
            {field: 'putgoodsOperateName', title: '客户名称'},
            {field: 'putgoodsName', title: '单位名称', style:'font-weight: 600;'},
            {field: 'unitNumber', title: '单位编号', style:'font-weight: 600;'},
            {field: 'putgoodsNotes', title: '配送注意事项'},
            {field: 'helpCode', title: '助记码'},
            {field: 'unitCategory', title: '单位类别'},
            {field: 'unitType', title: '单位类型'},
            {field: 'xlbClientId', title: '新零帮id'},
            {field: 'createTime', title: '创建时间'},
        ]];
    };

    // 单击行事件
    table.on('row(' + BasicWarehouse.tableId + ')', function (obj) {
        var data = obj.data;
        $('div[lay-id="' + BasicWarehouse.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
        $('div[lay-id="' + BasicWarehouse.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
        address(data);
    });
    function address(data){
        var warehouseId;
        var condition = '';
        if (data!=null) {
            $("#putgoodsId").val(data.id);
            warehouseId = data.id;
        } else {
            warehouseId = 0;
            condition = 'ndate';
        }
        // 渲染表格
        var tableResult = table.render({
            elem: '#' + BasicPutgoods.tableId,
            url: Feng.ctxPath + '/basicPutgoods/getStoreByWarehouseId?warehouseId='+warehouseId+"&condition="+condition,
            page: true,
            limit: 20,
            height: 370,
            toolbar: '#toolBarPutgoods',
            cellMinWidth: 100,
            cols: BasicPutgoods.initColumn()
        });

        // 添加按钮点击事件
        $('#btnAddPutgoods').click(function () {
            layer.open({
                type: 2,
                title: '添加门店',
                area: ['900px', '750px'],
                anim: 5,
                content: Feng.ctxPath + '/basicPutgoods/bindPutgoods?warehouseId='+warehouseId,
                end: function () {
                    address(data);
                }
            });
        });
        // 解绑门店
        BasicPutgoods.onUnbind = function (data2) {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/basicPutgoods/onUnbind", function (data2) {
                    console.log(data2)
                    if(data2.success){
                        Feng.success("解绑成功!");
                        address(data);
                    } else {
                        Feng.error("解绑失败!" + data2.message + "!");
                    }
                }, function (data2) {
                    Feng.error("解绑失败!" + data2.responseJSON.message + "!");
                });
                ajax.set("storeId", data2.id);
                ajax.set("warehouseId", data2.warehouseId);
                ajax.start();
            };
            Feng.confirm("是否解绑?", operation);
        };

        // 工具条点击事件
        table.on('tool(' + BasicPutgoods.tableId + ')', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'unbind') {
                BasicPutgoods.onUnbind(data);
            }
        });

        // // 修改参数状态
        // form.on('switch(switchWl)', function (obj) {
        //     var id = obj.elem.value;
        //     var checked = obj.elem.checked ? true : false;
        //
        //     BasicPutgoods.changeGradingStatus(id, checked);
        // });

        // /**
        //  * 修改参数状态
        //  *
        //  * @param id 参数id
        //  * @param checked 是否选中（true,false），选中就是启用用户，未选中就是禁用用户
        //  */
        // BasicPutgoods.changeGradingStatus = function (id, checked) {
        //     if (checked) {
        //         var ajax = new $ax(Feng.ctxPath + "/basicAddress/editItemswitchWl", function (data2) {
        //             Feng.success("启用成功!");
        //         }, function (data) {
        //             Feng.error("启用失败!");
        //             address(data);
        //         });
        //         ajax.set("switchWl", 1);
        //         ajax.set("id", id);
        //         ajax.start();
        //     } else {
        //         var ajax = new $ax(Feng.ctxPath + "/basicAddress/editItemswitchWl", function (data2) {
        //             Feng.success("禁用成功!");
        //         }, function (data) {
        //             Feng.error("禁用失败!" + data.responseJSON.message + "!");
        //             address(data);
        //         });
        //         ajax.set("switchWl", 2);
        //         ajax.set("id", id);
        //         ajax.start();
        //     }
        // };
    }

});
